package com.aw.guiguzi.module.setting

import android.widget.EditText
import com.aw.baselib.base.AwBaseActivity
import com.aw.baselib.base.AwBaseBindingActivity
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwSoftKeyboardUtil
import com.aw.guiguzi.R
import com.aw.guiguzi.databinding.ActivitySettingBettingCustomEditBinding
import com.aw.guiguzi.event.EventRefreshBettingCustomType
import com.aw.guiguzi.util.AppCacheUtil

class SettingBettingCustomEditActivity : AwBaseBindingActivity<ActivitySettingBettingCustomEditBinding>() {
    override fun getViewBinding(): ActivitySettingBettingCustomEditBinding {
        return ActivitySettingBettingCustomEditBinding.inflate(mActivity.layoutInflater)
    }

    override fun initView() {
        super.initView()
        setToolbarWithBackImgAndRightView("投注数量配置", "提交") {
            if (AwDataUtil.getIntValue(getText(binding.etSilverNum)) < 0 || AwDataUtil.getIntValue(
                    getText(
                        binding.etSilverNum
                    )
                ) > 10
            ) {
                showMsg("银数量需在0到10之间")
                return@setToolbarWithBackImgAndRightView
            }
            if (AwDataUtil.getIntValue(getText(binding.etSkinNum)) < 0 || AwDataUtil.getIntValue(
                    getText(
                        binding.etSkinNum
                    )
                ) > 10
            ) {
                showMsg("硬皮数量需在0到10之间")
                return@setToolbarWithBackImgAndRightView
            }
            if (AwDataUtil.getIntValue(getText(binding.etGemstoneNum)) < 0 || AwDataUtil.getIntValue(
                    getText(binding.etGemstoneNum)
                ) > 200
            ) {
                showMsg("宝石数量需在0到200之间")
                return@setToolbarWithBackImgAndRightView
            }
            AppCacheUtil.setBettingCustom(
                getText(binding.etSilverNum) + "/" + getText(binding.etSkinNum) + "/" + getText(
                    binding.etGemstoneNum
                )
            )
            postEvent(EventRefreshBettingCustomType(null))
            AwSoftKeyboardUtil.hideSoftKeyboard(mActivity)
            finish()
        }
    }

    override fun initData() {
        super.initData()
        val bettingCustomArrays =
            AppCacheUtil.getBettingCustom().split("/".toRegex()).dropLastWhile { it.isEmpty() }
                .toTypedArray()
        setText(binding.etSilverNum, bettingCustomArrays[0])
        setText(binding.etSkinNum, bettingCustomArrays[1])
        setText(binding.etGemstoneNum, bettingCustomArrays[2])
    }

    override fun initListener() {
        super.initListener()
    }
}